package com.zebra.common;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.ToString;

import java.io.UnsupportedEncodingException;

/**
 *
 * @Date 2020/7/7 0007
 **/
@Data
@ToString
public abstract class ZebraBaseMessage<T extends ZebraBaseMessage>{

    public abstract void write(ByteBuf b);

    public abstract T read(ByteBuf b);

    public byte[] readBytes(ByteBuf b) {
        return ByteBufUtils.readBytes(b);
    }

    public byte readByte(ByteBuf b) {
        return b.readByte();
    }
    public byte[] readByteArray(ByteBuf b){
        return ByteBufUtils.readByteArray(b);
    }

    public short readShort(ByteBuf b) {
        return b.readShort();
    }
    public short[] readShortArray(ByteBuf b) {
        return ByteBufUtils.readShortArray(b);
    }

    public float readFloat(ByteBuf b) {
        return b.readFloat();
    }
    public  float[] readFloatArray(ByteBuf b){
        return ByteBufUtils.readFloatArray(b);
    }

    public double readDouble(ByteBuf b) {
        return b.readDouble();
    }
    public double[] readDoubleArray(ByteBuf b) {
        return ByteBufUtils.readDoubleArray(b);
    }

    public int readInt(ByteBuf b) {
        return b.readInt();
    }
    public int[] readIntArray(ByteBuf b){
        return ByteBufUtils.readIntArray(b);
    }


    public long readLong(ByteBuf b) {
        return b.readLong();
    }
    public long[] readLongArray(ByteBuf b){
        return ByteBufUtils.readLongArray(b);
    }


    public String readString(ByteBuf b) {
        return ByteBufUtils.readString(b);
    }
    public String[] readStringArray(ByteBuf b){
        return ByteBufUtils.readStringArray(b);
    }


    public void writeBytes(ByteBuf b, byte[] v) {
        ByteBufUtils.writeBytes(b, v);
    }


    public void writeByte(ByteBuf b, byte v) {
        b.writeByte(v);
    }
    public void writeByteArray(ByteBuf b, byte[] vs){
        ByteBufUtils.writeByteArray(b, vs);
    }


    public void writeShort(ByteBuf b, short v) {
        b.writeShort(v);
    }
    public void writeShortArray(ByteBuf b, short[] vs){
        ByteBufUtils.writeShortArray(b, vs);
    }

    public void writeInt(ByteBuf b, int v) {
        b.writeInt(v);
    }
    public void writeIntArray(ByteBuf b, int[] vs){
        ByteBufUtils.writeIntArray(b, vs);
    }

    public void writeLong(ByteBuf b, long v) {
        b.writeLong(v);
    }
    public void writeLongArray(ByteBuf b, long[] vs){
        ByteBufUtils.writeLongArray(b, vs);
    }

    public void writeFloat(ByteBuf b, float v) {
        b.writeFloat(v);
    }
    public void writeFloatArray(ByteBuf b, float[] vs){
        ByteBufUtils.writeFloatArray(b, vs);
    }

    public void writeDouble(ByteBuf b, double v) {
        b.writeDouble(v);
    }

    public  void writeDoubleArray(ByteBuf b, double[] vs) {
       ByteBufUtils.writeDoubleArray(b, vs);
    }

    public void writeString(ByteBuf b, String v) {
       ByteBufUtils.writeString(b, v);
    }
    public void writeStringArray(ByteBuf b, String[] vs){
        ByteBufUtils.writeStringArray(b, vs);
    }

    public byte[] toByteArray(){
        Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String str = gson.toJson(this);
        try {
            return  str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            return new byte[0];
        }
    }


    public T parseFrom(byte[] bytes) {
        Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String jsonStr = null;
        try {
            jsonStr = new String(bytes,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
        return (T) gson.fromJson(jsonStr, this.getClass());
    }


}
